package ltd.dujiabao.activemq.jms;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import javax.jms.*;

/**
 * @author : DuJiabao
 * @Project : active-mq
 * @Time : 2021/5/4 23:07
 * @Desc :
 */
public class TopicPersistenceConsumer {
  private static final String ACTIVEMQ_URL = "tcp://192.168.233.134:61616";  // 服务器端口号，地址
  private static final String DESTINATION_NAME = "topic01";  // 目的地名称

  @Test
  public void test() throws Exception {
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.setClientID("Marry"); // 给自己的客户端起一个名字,用于标示
    connection.start();  // 开启连接

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(DESTINATION_NAME);
    TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subscriber");  // 这次不是注册消费者了
    // connection.start();  // 开启连接
    while (true) {
      Message message = subscriber.receive();
      if (message instanceof TextMessage) {
        TextMessage textMessage = (TextMessage) message;
        System.out.println("收到的持久化topic" + textMessage);
      } else break;
    }

    subscriber.close();
    session.close();
    connection.close();
  }
}
